<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
	</body>
	<script type="text/javascript">
		// 在线匹配 http://tool.oschina.net/regex/
		//. 所有单个字符(换行符除外) 
		var reg = /b..k/;   	// b和k之间有两个任意字符
		var str = "boooks";   //bok  book books boooks
		console.log(reg.test(str)); 
		
		//? 前面子表达式出现0次或1次  
		var reg = /bo?k/;  // o出现0次或1次
		var str = "book";  // bk bok book
		console.log(reg.test(str)); 
		
		//* 前面子表达式出现0次或多次
		var reg = /bo*k/;   // o出现0次或多次
		var str = "boook";  // bk bok book boook
		console.log(reg.test(str)); 
		
		//+ 前面子表达式出现1次或多次
		var reg = /bo+k/;
		var str = "boook";  // bk bok book boook
		console.log(reg.test(str)); 
		
		
		//{m,n}最少出现m次,最多出现n次   
		var reg = /bo{2,4}k/;
		var str = "boooook";  // bk bok book boook boooook
		console.log(reg.test(str)); 		
		
		//{m,}最少出现m次  
		var reg = /bo{2,}k/;
		var str = "boooook";  // bk bok book boook boooook
		console.log(reg.test(str)); 
		
		//{m}正好出现m次
        var reg = /bo{2}k/;
        var str = "boook";  // bk bok book boook 
        console.log(reg.test(str));		
		
		//[]限定某一位上字符的取值范围  
		//[abc]   匹配所包含的任意一个字符
        var reg = /o[usb]t/;  //只要包含usb一个,返回true
        var str = "obt";  // obt ost out
        console.log(reg.test(str));
		
		//[^xyz]  匹配未包含的任意字符
        var reg = /o[^usb]t/;  //只要包含usb一个,返回false
        var str = "oat";  // obt ost out
        console.log(reg.test(str));
		
        var reg = /[a-zA-Z0-9]ook/;  //限制第一位上必须是小写字母或大写字母或数字
        var str = "8ook";  // bk bok book boook 8ook
        console.log(reg.test(str));
		
		// [^]限定某一位上不能取的值
        var reg = /[^a-zA-Z]ook/;  //限制第一位上不能是小写字母
        var str = "book";  // bk bok book 8ook
        console.log(reg.test(str));
		
		
		//\d 等价于[0-9]  \D等价于[^0-9]  \w等价于[a-zA-Z0-9_]  \W等价于[^a-zA-Z0-9_]
        var reg = /\Dook/;  //限制第一位上必须是非数字
        var str = "8ook";  // book 8ook
        console.log(reg.test(str));
		
		
		//^匹配行首
		//$匹配行尾
        var reg = /book$/; 
        var str = "123 cook"; //book 123,   cook 123,  123 book,123 cook 
        console.log(reg.test(str));


		// \s  匹配空白字符,空格,制表符,换行符
        var reg = /java\sscript/; 
        var str = "study java script";  //study javascript
        console.log(reg.test(str));

		//this|where|logo 匹配this或where或logo中的任意一个
        var reg = /javascript|html|css/; 
        var str = "study javascript";
        console.log(reg.test(str));	
		
		//() 分组
        var reg = /javascript{2,4}/; // ==>t出现2到4次
        var reg = /(javascript){2,4}/;
        var str = "study javascriptjavascriptjavascript";
        console.log(reg.test(str));

        var reg = /(.*)\s(.*)/;   // $1 = (.*); $2 = (.*)
        var str = "hello world";
        console.log(str.replace(reg,'$2 $1'));  //将字符串掉个
		
		//贪婪 + * ? {m,n} {m,} {m}        尽可能多匹配,取最大匹配字符串
		//惰性 +? *? ?? {m,n}? {m,}? {m}?  尽可能少匹配,取最小匹配字符串
        var reg = /6(.*)6/;   //          /6(.*)6/   /6(.*?)6/
        var str = "study 6html6 6html6 6html6";
        var arr = reg.exec(str);
        console.log(arr);
        console.log(str.replace(reg,'$1'));  

		//m 匹配多行,换行模式
        var str = "1.baidu\n2.google\n3.IE";
        var reg = /^\d+/gm;  //加行首匹配   /\d+/g
        console.log(str.replace(reg,"#"));

		//验证手机号
        var reg = /^1[3578]\d{9}$/;
        var phone = "13523456789"
        console.log(reg.test(phone));
		
		//把手机号的中间显示为****
        var reg = /(\d{3})\d{4}(\d{4})/;
        var phone = "13523456789"
        console.log(phone.replace(reg,"$1****$2"));
		
		//验证是否是压缩文件
        var reg = /\w+\.[(rar)|(zip)|(gz)]/      //\w+ 任意个数字母数字  \.转义的点
        var  myfile = "text.rar";
        console.log(reg.test(myfile));
		
		//验证邮箱
		//行首  名字     多个@   域名           .  网址    行尾
		//[a-zA-Z0-9_\.\-]+  名字可以是字母数字下划线,点,杠,一次或多次
        var reg = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9_\.\-]+\.[a-zA-Z]+$/;    
        
        var email = "xiaoMing@qq.com";
        console.log(reg.test(email));
		
	</script>
</html>
